﻿using System.Collections.Generic;
using System.Linq;
using System.Web.UI;
using PaceR.Core.Interfaces;
using PaceR.Web.WebInterfaces;

namespace PaceR.Web.Renderers.Html
{
    public class SimpleMessageRenderer : IMessageRenderer
    {
        #region IMessageRenderer Members

        public void RenderMessages(HtmlTextWriter output, IEnumerable<IResultMessage> messages, string cssClass, ResultMessageTypes messageType)
        {
            if (messages != null && messages.Count() > 0)
            {
                output.WriteBeginTag("ul");
                output.WriteAttribute("class", GetMessageClass(messageType));
                output.Write(HtmlTextWriter.TagRightChar);
                foreach (var msg in messages)
                {
                    output.WriteBeginTag("li");
                    if (!string.IsNullOrEmpty(cssClass)) output.WriteAttribute("class", cssClass);
                    output.Write(HtmlTextWriter.TagRightChar);
                    output.Write(msg.ToString());
                    output.WriteEndTag("li");
                }
                output.WriteEndTag("ul");
            }
        }

        #endregion

        protected virtual string GetMessageClass(ResultMessageTypes messageType)
        {
            switch (messageType)
            {
                case ResultMessageTypes.Error:
                    return "error";

                case ResultMessageTypes.Warning:
                    return "warn";

                case ResultMessageTypes.Information:
                default:
                    return "info";
            }
        }
    }
}
